﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Linq;
using SDT.Libraries.Utilities;

namespace SDT.Libraries.TagRanking
{
    public class CountTags
    {
        private static int CountTagsAndSave(string tagText, int total, int articleID)
        {
            var objTagsDataContext = new lqTagsDataContext();
            try
            {
                int status = 0;
                status = objTagsDataContext.sp_WC_InsertTags(tagText, total, articleID);
                if (status > 0)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
//<Log Entry> : 10:44:36 AM Wednesday, February 23, 2011
//<Message>   :   Module SDT.Libraries.TagRanking.CountTags.CountTagsAndSave --Msg Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
//Cannot insert the value NULL into column 'TagID', table 'BDS.News.dbo.TagsRanking'; column does not allow nulls. INSERT fails.
//The statement has been terminated.
//The 'sp_WC_InsertTags' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead. 
            catch (Exception ex)
            {
                new LogException().Write(string.Format("  Module {0} --Msg {1} ", "SDT.Libraries.TagRanking.CountTags.CountTagsAndSave", ex.Message));
                return 0;
            }
            finally { objTagsDataContext.Dispose(); }
        }


        public static int UpdateMutipleKeywordTagRanking(Dictionary<string, int> dictTags, int articleID)
        {
            try
            {
                foreach (var s in dictTags)
                {
                    CountTagsAndSave(s.Key.Trim().ToString(), (int)s.Value, articleID);
                }
                return 1;
            }
            catch (Exception ex)
            {
                return 0;

            }
        }

        /// <summary>
        /// Author: TungNX
        /// Created Date : 22/2/1983
        /// Update mutiple tag in one connection
        /// 
        /// parameter : dictTags chứa tập hợp từ khóa và số lần xuất hiện trong bai viết
        /// parameter : articleID mã số tự tăng của bài viết
        /// 
        /// </summary>
        
        public int UpdateMutipleKeywordTagRankingOneConnection(Dictionary<string, int> dictTags, int articleID)
        {

            var objTagsDataContext = new lqTagsDataContext();
            try
            {
                int status = 0;
                foreach (var s in dictTags)
                {
                    status = objTagsDataContext.sp_WC_InsertTags(s.Key.ToString(), (int)s.Value, articleID);
                }
                //truong hop khong có exception tra ve true
                return 1;
            }

            catch (Exception ex)
            {
                new LogException().Write(string.Format("  Module {0} --Msg {1} ", "SDT.Libraries.TagRanking.CountTags.CountTagsAndSave", ex.Message));
               
            }
            finally { objTagsDataContext.Dispose(); }
            //truong hop exception tra ve false
            return 0;
        }



    }
}
